package fr.unice.progmobile.scratchpad;

import fr.unice.progmobile.scratchpad.adapter.ScratchpadAdapter;
import fr.unice.progmobile.scratchpad.db.ScratchpadOperations;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private final String CURRENT_SCRATCHPAD = "current";
	
	private ScratchpadOperations sop;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if (hasTabletFormFactor(this)) {
			
		} else {
			sop = new ScratchpadOperations(this);
			sop.open();
			if (sop.countScratchpad() < 1) {
				setContentView(R.layout.activity_main_empty);
				TextView tv = (TextView) findViewById(R.id.empty_ajouter);
				tv.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(MainActivity.this, AjouterActivity.class);
						startActivity(intent);
					}
				});
			} else {
				setContentView(R.layout.activity_main_filled);
				SimpleCursorAdapter adapter = new ScratchpadAdapter(this, sop.getScratchpads());
				final ListView lv = (ListView) findViewById(R.id.list_scratchpad);
				lv.setAdapter(adapter);
				lv.setOnItemClickListener(new OnItemClickListener() {
					
					
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						View v = lv.getAdapter().getView(arg2, findViewById(R.id.listitem_textview_id), null);
						Intent intent = new Intent(MainActivity.this, ConsulterActivity.class);
						intent.putExtra(CURRENT_SCRATCHPAD, Integer.parseInt(((TextView) v).getText().toString()));
						startActivity(intent);
					}
						
				});
			}
			sop.close();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case R.id.actionbar_ajouter:
			Intent intent = new Intent(MainActivity.this, AjouterActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public boolean hasTabletFormFactor(Context activityContext) {
		
	    boolean xlarge = 
	         ((activityContext.getResources().getConfiguration().screenLayout & 
	           Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE);

	    if (xlarge) {
	        DisplayMetrics metrics = new DisplayMetrics();
	        Activity activity = (Activity) activityContext;
	        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
	        
	        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
	                  || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
	                  || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM   
	                  || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {

	             return true;
	        }
	    }
	    return false;
	}
	
	@Override
	public void onBackPressed() {
		finish();
		super.onBackPressed();
	}
}
